﻿using System;

namespace AdHocReports
{
    public static class Conversion
    {
        private static byte[] byKey1 = System.Text.ASCIIEncoding.ASCII.GetBytes("55CCC9B3");
        private static byte[] byIV2 = System.Text.ASCIIEncoding.ASCII.GetBytes("B1BB65E85F095C83");

        public static string Encrypt(ref string sIn)
        {
            byte[] byRet = null;
            byte[] byIn = System.Text.ASCIIEncoding.ASCII.GetBytes(sIn);
            sIn = "";
            sIn = null;

            try
            {
                byRet = EncryptRijndael(ref byIn);
                for (int i = 0; i < byIn.Length; i++)
                {
                    byIn[i] = 0;
                }
                byIn = null;
            }
            catch
            {
                return "";
            }

            return GetHexStringFromByteArray(ref byRet);
        }

        public static string Decrypt(ref string sIn)
        {
            byte[] byRet = GetByteArrayFromHexString(ref sIn);
            sIn = "";
            sIn = null;

            try
            {
                byRet = DecryptRijndael(ref byRet);
            }
            catch
            {
                return "";
            }

            return System.Text.ASCIIEncoding.ASCII.GetString(byRet);
        }

        private static byte[] EncryptRijndael(ref byte[] byIn)
        {
            byte[] byRet = null;
            System.IO.MemoryStream ioms = new System.IO.MemoryStream();
            System.Security.Cryptography.RijndaelManaged csp = new System.Security.Cryptography.RijndaelManaged();
            System.Security.Cryptography.CryptoStream csOut = new System.Security.Cryptography.CryptoStream(ioms, csp.CreateEncryptor(byKey1, byIV2), System.Security.Cryptography.CryptoStreamMode.Write);

            csOut.Write(byIn, 0, byIn.Length);
            csOut.Close();
            csOut = null;
            csp.Clear();
            csp = null;
            byRet = ioms.ToArray();
            ioms.Close();
            ioms = null;

            return byRet;
        }

        private static byte[] DecryptRijndael(ref byte[] byIn)
        {
            byte[] byRet = null;
            System.IO.MemoryStream ioms = new System.IO.MemoryStream();
            System.Security.Cryptography.RijndaelManaged csp = new System.Security.Cryptography.RijndaelManaged();
            System.Security.Cryptography.CryptoStream csOut = new System.Security.Cryptography.CryptoStream(ioms, csp.CreateDecryptor(byKey1, byIV2), System.Security.Cryptography.CryptoStreamMode.Write);

            csOut.Write(byIn, 0, byIn.Length);
            csOut.Close();
            csOut = null;
            csp.Clear();
            csp = null;
            byRet = ioms.ToArray();
            ioms.Close();
            ioms = null;

            return byRet;
        }

        private static byte[] GetByteArrayFromHexString(ref string sIn)
        {
            byte[] byRet = new byte[(sIn.Length / 2)];
            int i = 0;
            int j = 0;

            for (i = 0; i < sIn.Length; i += 2)
            {
                byRet[j] = System.Convert.ToByte(System.Convert.ToUInt32(sIn[i].ToString() + sIn[i + 1].ToString(), 16).ToString());
                j++;
            }

            return byRet;
        }

        private static string GetHexStringFromByteArray(ref byte[] byIn)
        {
            string sRet = "";
            int i = 0;

            for (i = 0; i < byIn.Length; i++)
            {
                sRet += String.Format("{0:x2}", System.Convert.ToUInt32(byIn[i].ToString()));
            }

            return sRet.ToUpper();
        }
    }
}
